//
//  HMStatusOriginalView.m
//  SinaWeiBo
//
//  Created by WGP on 16/4/16.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "HMStatusOriginalView.h"
#import "HMStatuses.h"
#import "HMUser.h"
#import "HMStatusPictureView.h"
#import "UIImage+AsyncDraw.h"

@interface HMStatusOriginalView ()

@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UIImageView *vipImageView;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UILabel *sourceLabel;
@property(nonatomic,strong)UILabel *contentLabel;
@property(nonatomic,strong)UIImageView *verifierImageView;
@property(nonatomic,strong)HMStatusPictureView *pictureView;

@end

@implementation HMStatusOriginalView

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self setupUI];

    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
    {
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    self.backgroundColor = [UIColor whiteColor];
//    1.添加控件
    self.iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar" ]];
    self.iconView.layer.cornerRadius = 17.5;
    self.iconView.layer.masksToBounds = YES;
    [self addSubview:self.iconView];
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.textColor = [UIColor darkGrayColor];
    self.nameLabel.font = [UIFont systemFontOfSize:HMNameLabelFontSize];
    [self addSubview:self.nameLabel];
    
    self.vipImageView = [[UIImageView alloc] init];
    [self addSubview:self.vipImageView];
    
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.font = [UIFont systemFontOfSize:HMTimeLabelFontSize];
    self.timeLabel.textColor = [UIColor orangeColor];
    [self addSubview:self.timeLabel];
    
    self.sourceLabel = [[UILabel alloc] init];
    self.sourceLabel.textColor = [UIColor lightGrayColor];
    self.sourceLabel.font = [UIFont systemFontOfSize:HMTimeLabelFontSize];
    [self addSubview:self.sourceLabel];
    
    self.verifierImageView = [[UIImageView alloc] init];
    [self addSubview:self.verifierImageView];
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.font = [UIFont systemFontOfSize:HMContentLabelFontSize];
    self.contentLabel.textColor = [UIColor darkGrayColor];
    self.contentLabel.numberOfLines = 0;
    [self addSubview:self.contentLabel];
    
    self.pictureView = [[HMStatusPictureView alloc] init];
    [self addSubview:self.pictureView];
    
//    2.添加约束
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(35);
        make.left.top.equalTo(self).offset(HMStatusCellMargin);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView);
        make.left.equalTo(self.iconView.mas_right).offset(HMStatusCellMargin);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.bottom.equalTo(self.iconView.mas_bottom);
    }];
    
    [self.vipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel);
        make.left.equalTo(self.nameLabel.mas_right).offset(HMStatusCellMargin);
    }];
    
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.timeLabel);
        make.left.equalTo(self.timeLabel.mas_right).offset(HMStatusCellMargin);
        
    }];
    
    [self.verifierImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_right).offset(-4);
        make.centerY.equalTo(self.iconView.mas_bottom).offset(-4);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_bottom).offset(HMStatusCellMargin);
        make.left.equalTo(self.iconView);
        make.right.equalTo(self).offset(-8);
    }];
    
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(HMStatusCellMargin);
        make.left.equalTo(self).offset(HMStatusCellMargin);
        make.size.mas_equalTo(CGSizeZero);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.pictureView).offset(HMStatusCellMargin);
    }];
}

-(void)setStatus:(HMStatuses *)status
{
    _status = status;
    
    [self.iconView sd_setImageWithURL:self.status.user.profileImageURL placeholderImage:[UIImage imageNamed:@"avatar"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [image WP_AsyncDrawImageWith:image.size bgColor:[UIColor whiteColor] isCorner:YES finished:^(UIImage *image) {
            self.iconView.image = image;
        }];
    }];
    
    self.nameLabel.text = self.status.user.screen_name;
//    self.vipImageView.image = [UIImage imageNamed:self.status.user.mbrankImageName];
    UIImage *vipImage = [UIImage imageNamed:self.status.user.mbrankImageName];
    [vipImage WP_AsyncDrawImageWith:vipImage.size bgColor:[UIColor whiteColor] isCorner:NO finished:^(UIImage *image) {
        self.vipImageView.image = image;
    }];
    
//    self.timeLabel.text = self.status.created_at;
    self.timeLabel.text = [[NSDate dateWith:self.status.created_at] sinaDateDescription];
    
    self.sourceLabel.text = self.status.source;
    self.contentLabel.text = self.status.text;
    self.verifierImageView.image = [UIImage imageNamed:self.status.user.verified_typeName];
    
    self.pictureView.pic_urlsArr = status.pic_urlsArr;
    
    [self.pictureView mas_updateConstraints:^(MASConstraintMaker *make) {
        CGFloat margin = status.pic_urlsArr.count == 0 ? 0 : HMStatusCellMargin;
        make.top.equalTo(self.contentLabel.mas_bottom).offset(margin);
    }];
}

@end













